<?php

/**
* alltosun.com 收藏公共函数 fav_helper.php
* ============================================================================
* 版权所有 (C) 2009-2012 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 钱有明 (qianym@alltosun.com) $
* $Date: 2012-10-8 下午12:29:00 $
* $Id: fav_helper.php 22491 2013-04-08 07:43:17Z weisd $
*/

class fav_helper
{
    public static function get_fav_num_by_user($user_id, $res_name = 'goods')
    {
        if (!$user_id) {
            return false;
        }

        return _model('fav')->getTotal(array('user_id'=>$user_id, 'res_name'=>$res_name, 'status'=>1));
    }

    public static function get_fav_by_user($goods_id, $user_id)
    {
        if (!$goods_id) {
            return false;
        }

        if (!$user_id) {
            return false;
        }

        $info = array();
        $info = _uri('fav', array('user_id'=>$user_id, 'res_id'=>$goods_id, 'res_name'=>'goods'));
        if (!$info) {
            return false;
        }
        return true;
    }

    /**
     * 取得单条收藏信息
     * @param int $fav_id
     * @return array
     */
    public static function get_fav_info($fav_id)
    {
        if (empty($fav_id)) {
            return array();
        }

        return _uri('fav', (int)$fav_id);
    }

    /**
     * 通过相应的条件，取得收藏的id，以供在各app的收藏列表中做删除使用
     * @param array $params
     */
    public static function get_fav_id($params)
    {
        $filter = array();
        $filter['res_name'] = '';
        $filter['res_id'] = 0;
        $filter['user_id'] = 0;

        foreach ($params as $k=>$v) {
            if (array_key_exists($k, $filter)) {
                $filter[$k] = $v;
            }
        }

        return _uri('fav', $filter, 'id');
    }

    /**
     * 取得某一资源被收藏的次数
     * @param string $res_name
     * @param int $res_id
     * @return int
     */
    public static function get_res_fav_count($res_name, $res_id)
    {
        if (empty($res_name) || empty($res_id)) {
            return 0;
        }

        $filter = array();
        $filter['res_name'] = $res_name;
        $filter['res_id'] = $res_id;
        return _model('fav')->getTotal($filter);
    }

    /**
     * 判断当前用户是否已收藏了某资源
     * @param string $res_name
     * @param int $res_id
     * @return boolean
     */
    public static function is_faved($res_name, $res_id, $user_id = '')
    {
        if (!$user_id) {
            $user_id = user_helper::get_user_id();
        }

        $fav_ids = fav_helper::get_res_ids($res_name, $user_id);
        if (empty($fav_ids)) {
            return FALSE;
        }

        if (in_array($res_id, $fav_ids)) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    /**
     * 取得用户收藏的某一类资源所有ids
     * @param string $res_name
     * @param int $user_id
     * @return array ids
     */
    public static function get_res_ids($res_name, $user_id)
    {
        if (empty($res_name)) {
            return array();
        }

        if (empty($user_id)) {
            $user_id = user_helper::get_user_id();
        }

        $filter = array();
        $filter['res_name'] = $res_name;
        $filter['user_id'] = $user_id;

        return _model('fav')->getFields('res_id', $filter);
    }

    /**
     * 取得某一资源的所有被哪些人收藏
     * @param string $res_name
     * @param int $res_id
     * @return array user_ids
     */
    public static function get_user_ids($res_name, $res_id)
    {
        if (empty($res_name)) {
            return array();
        }

        if (empty($res_id)) {
            return array();
        }

        $filter['res_name'] = $res_name;
        $filter['res_id'] = $res_id;

        return _model('fav')->getFields('user_id', $filter);
    }

    /**
     * 判断是否在可收藏的资源内
     * @param string $res_name
     * @return boolean
     */
    public static function isset_fav_resname_config($res_name)
    {
            return array_key_exists($res_name, fav_config::$fav_res_name);
    }
}
?>